<template>
  <div class="student-list-container">
    <div class="page-header">
      <h1>学生信息列表</h1>
      <div class="header-decoration"></div>
      <el-button type="danger" @click="handleLogout" class="logout-btn">退出登录</el-button>
    </div>
    <el-card class="student-card">
      <el-table 
        :data="students" 
        style="width: 100%" 
        @row-click="handleRowClick" 
        :row-class-name="tableRowClassName"
        :header-cell-style="headerCellStyle"
        :cell-style="cellStyle"
        border
        stripe>
        <el-table-column prop="name" label="姓名" width="180">
          <template #header>
            <span class="column-header">姓名</span>
          </template>
        </el-table-column>
        <el-table-column prop="id" label="学号" width="180">
          <template #header>
            <span class="column-header">学号</span>
          </template>
        </el-table-column>
        <el-table-column prop="average" label="平均分">
          <template #header>
            <span class="column-header">平均分</span>
          </template>
        </el-table-column>
        <el-table-column prop="total" label="总成绩">
          <template #header>
            <span class="column-header">总成绩</span>
          </template>
        </el-table-column>
        <el-table-column prop="rank" label="排名">
          <template #header>
            <span class="column-header">排名</span>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div> 
</template>

<script>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';

export default {
  setup() {
    const router = useRouter();
    const students = ref([]);
    
    const fetchStudents = async () => {
      try {
        const token = localStorage.getItem('token');
        const response = await fetch('http://localhost:3000/students', {
          headers: {
            'Authorization': `Bearer ${token}`
          }
        });
        
        if (!response.ok) {
          throw new Error('获取数据失败');
        }
        
        const data = await response.json();
        
        // 按总成绩从高到低排序
        const sortedStudents = data
          .map(student => ({
            ...student,
            totalScore: student.totalScore || 0
          }))
          .sort((a, b) => b.totalScore - a.totalScore);
          
        // 计算排名
        students.value = sortedStudents.map((student, index) => ({
          name: student.name,
          id: student.studentId,
          average: student.totalScore / 5,
          total: student.totalScore,
          rank: index + 1
        }));
      } catch (error) {
        console.error('获取学生数据失败:', error);
        ElMessage.error('获取学生数据失败');
      }
    };
    
    onMounted(() => {
      fetchStudents();
    });
    
    const handleRowClick = (row) => {
      router.push({ name: 'StudentDetail', params: { id: row.id } });
    };

    const handleLogout = () => {
      localStorage.removeItem('token');
      localStorage.removeItem('user');
      router.push('/login');
      ElMessage.success('退出登录成功');
    };

    const tableRowClassName = ({ row, rowIndex }) => {
      if (rowIndex % 2 === 0) {
        return 'even-row';
      } else {
        return 'odd-row';
      }
    };
    
    return {
      students,
      handleRowClick,
      handleLogout,
      tableRowClassName
    };
  }
};
</script>

<style scoped>
.student-list-container {
  padding: 2rem;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4e7eb 100%);
  min-height: 100vh;
}

.page-header {
  text-align: center;
  margin-bottom: 2.5rem;
  position: relative;
}

h1 {
  font-size: 2.2rem;
  color: #2c3e50;
  font-weight: 600;
  margin-bottom: 1rem;
  letter-spacing: 0.5px;
}

.header-decoration {
  width: 60px;
  height: 4px;
  background: linear-gradient(90deg, #409EFF, #67C23A);
  margin: 0 auto;
  border-radius: 2px;
}

.student-card {
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
}

.student-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 25px rgba(0, 0, 0, 0.12);
}

:deep(.el-table) {
  border-radius: 8px;
  overflow: hidden;
}

:deep(.el-table th) {
  background-color: #f8fafc !important;
  font-weight: 600;
  color: #2c3e50;
}

:deep(.el-table td) {
  padding: 12px 0;
}

.column-header {
  font-weight: 600;
  color: #2c3e50;
}

:deep(.even-row) {
  background-color: #f8fafc;
}

:deep(.odd-row) {
  background-color: #ffffff;
}

:deep(.el-table__row:hover) {
  background-color: #f0f7ff !important;
  cursor: pointer;
}

@media (max-width: 768px) {
  .student-list-container {
    padding: 1rem;
  }
  
  h1 {
    font-size: 1.8rem;
  }
  
  .student-card {
    margin: 0 -0.5rem;
  }
}

.logout-btn {
  position: absolute;
  right: 20px;
  top: 20px;
}
</style>